package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Content;
import dao.Annual_reportDao;
import dao.Campus_constructionDao;
import dao.Funding_informationDao;
import dao.Fundraising_projectDao;
import dao.ManageDao;
import dao.NewsDao;
import dao.Photo_newsDao;
import dao.ThanksDao;

public class WriteServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String id = req.getParameter("id");
		String modifyTitle = req.getParameter("modifyTitle");
		String data = req.getParameter("edt1");

		if (data != null) {
			data = data.replaceAll("\r\n", "");
			data = data.replaceAll("\r", "");
			data = data.replaceAll("\n", "");
			data = data.replaceAll("\"", "'");
		}
		
		HttpSession session = req.getSession();

		String choice_id2 = (String) session.getAttribute("choice_id");
		Content content = (Content) session.getAttribute("content");
		content.setTitle(modifyTitle);
		content.setData(data);
		
		session.setAttribute("content", content);
		
		int choice_id = Integer.parseInt(choice_id2);

		int num = 0;
		switch (choice_id) {
		case 1:
			num = new Photo_newsDao().updateContent(id, modifyTitle, data);
			break;
		case 2:
			num = new NewsDao().updateContent(id, modifyTitle, data);
			break;
		case 3:
			num = new Funding_informationDao().updateContent(id, modifyTitle,
					data);
			break;
		case 4:
			break;
		case 5:
			num = new ThanksDao().updateContent(id, modifyTitle, data);
			break;
		case 6:
			num = new ManageDao().updateContent(id, modifyTitle, data);
			break;
		case 7:
			num = new Annual_reportDao().updateContent(id, modifyTitle, data);
			break;
		case 8:
			num = new Fundraising_projectDao().updateContent(id, modifyTitle,
					data);
			break;
		case 9:
			num = new Campus_constructionDao().updateContent(id, modifyTitle, data);
			break;
		}

		if (num == 1) {
			session.setAttribute("find_insert_id", 4);
		}else {
			session.setAttribute("find_insert_id", 5);
		}
		
		resp.sendRedirect("houtai/guanli_1.jsp?id=" + choice_id);
	}
}
